/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dale2507.gates.gate;

import dale2507.gates.data.Settings;
import org.bukkit.Material;
import org.bukkit.inventory.ItemStack;

/**
 * Item used in an address.
 * @author Dale2507
 */
public class AddressItem {
    
    /**
     * Material of address item
     */
    private Material material;
    /**
     * Subid of material if available (Used to define color mostly)
     */
    private short subid = 0;
    /**
     * Amount of item
     */
    private int amount = 1;

    /**
     * Material only constructor. Uses default values for subid and amount.
     * @param material Material of address item
     */
    public AddressItem(Material material) {
        this.material = material;
    }
    
    /**
     * Material and amount constructor. Uses default values for subid.
     * @param material Material of address item
     * @param amount Amount of item
     */
    public AddressItem(Material material, int amount) {
        this(material);
        if (Settings.getInstance().connection.checkAmount()) this.amount = amount;
    }

    /**
     * Material and subid constructor. Uses default values for amount.
     * @param material Material of address item
     * @param subid SubId of the item
     */
    public AddressItem(Material material, short subid) {
        this(material);
        if (this.material.getMaxDurability() == 0) this.subid = subid;
    }
    
    /**
     * Full constructor
     * @param material Material of address item
     * @param amount Amount of item
     * @param subid SubId of the item
     */
    public AddressItem(Material material, int amount, short subid) {
        this(material, amount);
        if (this.material.getMaxDurability() == 0) this.subid = subid;
    }

    /**
     * Ease of use constructor. Generates address item by item stack.
     * @param stack Item stack to generate address from.
     */
    public AddressItem(ItemStack stack) {
        this(stack.getType(), stack.getAmount(), stack.getDurability());
    }

    /**
     * Retrieves material of address item
     * @return Material of address item
     */
    public Material getMaterial() {
        return material;
    }

    /**
     * Retrieves subid of address item
     * @return SubID of address item
     */
    public short getSubid() {
        return subid;
    }

    /**
     * Retrieves number of items. Required only if @see USE_AMOUNT is true
     * @return 
     */
    public int getAmount() {
        return amount;
    }

    @Override
    public boolean equals(Object obj) {
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        final AddressItem other = (AddressItem) obj;
        if (this.material.getId() != other.material.getId()) {
            return false;
        }
        if (this.subid != other.subid) {
            return false;
        }
        if (this.amount != other.amount) {
            return false;
        }
        return true;
    }

    @Override
    public int hashCode() {
        int hash = 7;
        hash = 89 * hash + (this.material != null ? this.material.hashCode() : 0);
        hash = 89 * hash + this.subid;
        return hash;
    }

    @Override
    public String toString() {
        return material.toString() + ":" + subid + " (" + amount + ")";
    }
    
}
